//
//  ZJPhoneTextFliedView.m
//  GongXinDangJian
//
//  Created by ZJ on 2017/12/11.
//  Copyright © 2017年 apple. All rights reserved.
//

#import "ZJPhoneTextFliedView.h"

@implementation ZJPhoneTextFliedView

//默认高度90

- (instancetype)initWithFrame:(CGRect)frame  title:(NSString *)title
{
    self = [super initWithFrame:frame title:title];
    if (self) {
        self.titleLabel.frame = CGRectMake(12 * [UIScreen zj_screenWScale], self.titleLabel.y, self.width - 24 * [UIScreen zj_screenWScale], self.titleLabel.height);
        [self addSubview:self.textField];
        [self addSubview:self.textField1];
    }
    return self;
}


- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    ///只能输入成功
    BOOL isInt = [NSString validateNumber:string];
    if (!isInt) {//不是数字
        return NO;
    }
    NSString * toBeString = [textField.text stringByReplacingCharactersInRange:range withString:string];
    
    
    if (textField == self.textField) {
        if (toBeString.length >= 4 && range.length!= 1 && 4 > 0){
            textField.text = [toBeString substringToIndex:4];
            return NO;
        }
    }
    if (textField == self.textField1) {
        if (toBeString.length >= 8 && range.length!= 1 && 8 > 0){
            textField.text = [toBeString substringToIndex:8];
            return NO;
        }
    }
    
    return YES;
}





- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    
    [textField resignFirstResponder];//取消第一响应者
    
    return YES;
}
#pragma mark - getter
- (UITextField *)textField{
    if (_textField == nil){
        _textField = [[UITextField alloc] initWithFrame:CGRectMake(self.titleLabel.x, self.titleLabel.bottom, 100 * [UIScreen zj_screenWScale], 44 * [UIScreen zj_screenHScale])];
        [_textField setTextFieldLeftforWidth:13 * [UIScreen zj_screenWScale]];
        [_textField setTextFieldRightforWidth:13 * [UIScreen zj_screenWScale]];
        _textField.placeholder = @"请输入内容...";
        _textField.textColor = [UIColor textBlackColor];
        _textField.font = [UIFont titleTextFont];
        _textField.layer.cornerRadius = normalCornerRadius;
        _textField.layer.borderColor = [UIColor textBlackColor].CGColor;
        _textField.layer.borderWidth = normalBorderWidth;
        _textField.returnKeyType = UIKeyboardTypeTwitter;
        _textField.textAlignment = NSTextAlignmentCenter;
        _textField.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
        _textField.delegate = self;
    }
    return _textField;
}
- (UITextField *)textField1{
    if (_textField1 == nil){
        _textField1 = [[UITextField alloc] initWithFrame:CGRectMake(self.textField.right + 10 * [UIScreen zj_screenWScale], self.titleLabel.bottom, self.width -  self.titleLabel.x - self.textField.right - 10 * [UIScreen zj_screenWScale], 44 * [UIScreen zj_screenHScale])];
        [_textField1 setTextFieldLeftforWidth:13 * [UIScreen zj_screenWScale]];
        [_textField1 setTextFieldRightforWidth:13 * [UIScreen zj_screenWScale]];
        _textField1.placeholder = @"请输入内容...";
        _textField1.textColor = [UIColor textBlackColor];
        _textField1.font = [UIFont titleTextFont];
        _textField1.layer.cornerRadius = normalCornerRadius;
        _textField1.layer.borderColor = [UIColor textBlackColor].CGColor;
        _textField1.layer.borderWidth = normalBorderWidth;
        _textField1.returnKeyType = UIKeyboardTypeTwitter;
        _textField1.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
        _textField1.delegate = self;
    }
    return _textField1;
}

@end
